<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon_active"></slot>
    </div>
    <!-- 插槽会被替换，所有类名给父盒子div 颜色会继承 -->
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  // 动态传入path
  props: {
    path: String,
    activeColor: {
      type: String,
      // 默认颜色
      default: "#317ffb",
    },
  },
  data() {
    return {
      // isActive: false,
    };
  },
  computed: {
    isActive() {
      // this.$route 哪个路由处于活跃 就是哪个
      // indexOf === -1 表示里面没找到这个路径
      return this.$route.path.indexOf(this.path) !== -1;
    },
    activeStyle() {
      return this.isActive ? { color: this.activeColor } : {};
    },
  },
  methods: {
    itemClick() {
      this.$router.replace(this.path).catch((err) => err);
    },
  },
};
</script>

<style>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  font-size: 15px;
}
.tab-bar-item span {
  display: inline-block;
  width: 23px;
  height: 23px;
  font-size: 20px;
  margin: 3px 0;
  vertical-align: middle;
}
</style>